package com.example.prison.controller;//package com.example.military.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.DeviceManagement;
import com.example.prison.service.DeviceManagementService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api("设备管理")
@RestController
@RequestMapping("/api")
public class DeviceManagementController {

    @Autowired
    private DeviceManagementService deviceManagementService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取设备列表")
    @GetMapping("/getDeviceList")
    public Response<PageInfo<DeviceManagement>> getDeviceList(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                          @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                              @Parameter(description = "设备类型")String type,
                                                              @Parameter(description = "设备编号")String deviceNumber) {
        PageInfo<DeviceManagement> pageInfo = deviceManagementService.list(pageNumber, pageSize, type, deviceNumber);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("获取设备列表")
    @GetMapping("/getDevice")
    public Response<PageInfo<DeviceManagement>> getDevice(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                          @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        PageInfo<DeviceManagement> pageInfo = deviceManagementService.get(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增设备")
    @PostMapping("/addDevice")
    public Response<?> addDevice(@RequestBody DeviceManagement device) {
        int result = deviceManagementService.add(device);
        if (result == -1) {
            return Response.error("设备已存在！");
        }
        log.info("新增设备"+", 数据："+ JSON.toJSONString(device));
        systemLogService.add("新增设备");
        return Response.success("添加成功！");
    }

    @ApiOperation("删除设备")
    @PostMapping("/deleteDevice")
    public Response<?> deleteDevice(@Parameter(description = "设备ID") @RequestBody Integer[] ids) {
        deviceManagementService.delete(ids);
        log.info("删除设备");
        systemLogService.add("删除设备");
        return Response.success("删除成功！");
    }

    @ApiOperation("编辑设备")
    @PostMapping("/updateDevice")
    public Response<?> updateDevice(@RequestBody DeviceManagement device) {
        Integer id = device.getId();
        int result = deviceManagementService.update(device, id);
        if (result == -1) {
            return Response.error("设备编号已存在！");
        }
        log.info("编辑设备"+", 数据："+JSON.toJSONString(device));
        systemLogService.add("编辑设备");
        return Response.success("更新成功！");
    }

    @ApiOperation("依据id获取设备信息")
    @GetMapping("/getDeviceById")
    public Response<DeviceManagement> getDeviceById(@Parameter(description = "设备ID")@RequestParam Integer deviceId) {
        DeviceManagement device = deviceManagementService.getById(deviceId);
        if(ObjectUtils.isEmpty(device)) {
            return Response.error("此设备id不存在！");
        }
        return Response.success("获取成功！", device);
    }
//
//    @ApiOperation("依据设备类型获取设备信息")
//    @GetMapping("/getDeviceByType")
//    public Response<PageInfo<DeviceManagement>> getDeviceByType(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                                                @Parameter(description = "设备类型")@RequestParam String type) {
//        PageInfo<DeviceManagement> pageInfo = deviceManagementService.getByType(pageNumber, pageSize, type);
//        if(ObjectUtils.isEmpty(pageInfo.getList())) {
//            return Response.error("设备不存在！");
//        }
//        return Response.success("获取成功！", pageInfo);
//    }
//
//    @ApiOperation("依据设备编号获取设备信息")
//    @GetMapping("/getDeviceByNumber")
//    public Response<PageInfo<DeviceManagement>> getDeviceByNumber(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                                                  @Parameter(description = "设备编号")@RequestParam String deviceNumber) {
//        PageInfo<DeviceManagement> pageInfo = deviceManagementService.getByNumber(pageNumber, pageSize, deviceNumber);
//        if(ObjectUtils.isEmpty(pageInfo.getList())) {
//            return Response.error("设备不存在！");
//        }
//        return Response.success("获取成功！", pageInfo);
//    }

}
